import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

// 模拟数据
const mockCourses = [
  {
    id: 1,
    code: 'CS101',
    name: '计算机导论',
    teacher: '张教授',
    credits: 3,
    type: '必修课',
    capacity: 50,
    enrolled: 32,
    schedule: [
      { day: 1, startTime: '8:00', endTime: '9:40', location: '教学楼A-101' },
      { day: 3, startTime: '8:00', endTime: '9:40', location: '教学楼A-101' }
    ],
    description: '本课程介绍计算机科学的基本概念和原理，包括计算机历史、硬件、软件、网络等内容。'
  },
  {
    id: 2,
    code: 'CS102',
    name: '程序设计基础',
    teacher: '李教授',
    credits: 4,
    type: '必修课',
    capacity: 40,
    enrolled: 38,
    schedule: [
      { day: 2, startTime: '10:00', endTime: '11:40', location: '教学楼B-202' },
      { day: 4, startTime: '10:00', endTime: '11:40', location: '教学楼B-202' }
    ],
    description: '本课程讲授程序设计的基本方法和技巧，使用Java语言进行实践。'
  },
  {
    id: 3,
    code: 'CS201',
    name: '数据结构',
    teacher: '王教授',
    credits: 4,
    type: '必修课',
    capacity: 45,
    enrolled: 40,
    schedule: [
      { day: 1, startTime: '14:00', endTime: '15:40', location: '教学楼C-303' },
      { day: 3, startTime: '14:00', endTime: '15:40', location: '教学楼C-303' }
    ],
    description: '本课程介绍各种基本数据结构及其操作算法，包括线性表、栈、队列、树、图等。'
  },
  {
    id: 4,
    code: 'CS301',
    name: '数据库系统',
    teacher: '赵教授',
    credits: 3,
    type: '必修课',
    capacity: 40,
    enrolled: 25,
    schedule: [
      { day: 2, startTime: '14:00', endTime: '15:40', location: '教学楼A-204' },
      { day: 5, startTime: '10:00', endTime: '11:40', location: '教学楼A-204' }
    ],
    description: '本课程介绍数据库系统的基本概念、设计方法和实现技术。'
  },
  {
    id: 5,
    code: 'CS401',
    name: '人工智能导论',
    teacher: '钱教授',
    credits: 3,
    type: '选修课',
    capacity: 35,
    enrolled: 20,
    schedule: [
      { day: 1, startTime: '10:00', endTime: '11:40', location: '教学楼B-305' },
      { day: 4, startTime: '14:00', endTime: '15:40', location: '教学楼B-305' }
    ],
    description: '本课程介绍人工智能的基本概念、方法和应用，包括搜索算法、知识表示、机器学习等内容。'
  },
  {
    id: 6,
    code: 'CS402',
    name: '软件工程',
    teacher: '孙教授',
    credits: 4,
    type: '必修课',
    capacity: 45,
    enrolled: 30,
    schedule: [
      { day: 3, startTime: '10:00', endTime: '11:40', location: '教学楼C-106' },
      { day: 5, startTime: '14:00', endTime: '15:40', location: '教学楼C-106' }
    ],
    description: '本课程介绍软件开发的过程、方法和工具，包括需求分析、设计、编码、测试等内容。'
  }
]

export const useCourseStore = defineStore('course', () => {
  // 状态
  const courses = ref(mockCourses)
  const selectedCourses = ref(JSON.parse(localStorage.getItem('selectedCourses')) || [])
  const searchKeyword = ref('')
  
  // 计算属性
  const filteredCourses = computed(() => {
    if (!searchKeyword.value) return courses.value
    const keyword = searchKeyword.value.toLowerCase()
    return courses.value.filter(
      course => 
        course.name.toLowerCase().includes(keyword) || 
        course.code.toLowerCase().includes(keyword) ||
        course.teacher.toLowerCase().includes(keyword)
    )
  })
  
  const selectedCourseIds = computed(() => selectedCourses.value.map(course => course.id))
  
  // 操作
  function selectCourse(course) {
    if (!selectedCourseIds.value.includes(course.id)) {
      selectedCourses.value.push(course)
      saveCourses()
    }
  }
  
  function dropCourse(courseId) {
    selectedCourses.value = selectedCourses.value.filter(course => course.id !== courseId)
    saveCourses()
  }
  
  function saveCourses() {
    localStorage.setItem('selectedCourses', JSON.stringify(selectedCourses.value))
  }
  
  function addCourse(course) {
    const maxId = Math.max(...courses.value.map(c => c.id), 0)
    const newCourse = {
      ...course,
      id: maxId + 1
    }
    courses.value.push(newCourse)
  }
  
  function updateCourse(course) {
    const index = courses.value.findIndex(c => c.id === course.id)
    if (index !== -1) {
      courses.value[index] = course
    }
  }
  
  function deleteCourse(courseId) {
    courses.value = courses.value.filter(course => course.id !== courseId)
    // 从已选课程中也删除
    if (selectedCourseIds.value.includes(courseId)) {
      dropCourse(courseId)
    }
  }
  
  function setSearchKeyword(keyword) {
    searchKeyword.value = keyword
  }
  
  return {
    courses,
    selectedCourses,
    searchKeyword,
    filteredCourses,
    selectedCourseIds,
    selectCourse,
    dropCourse,
    addCourse,
    updateCourse,
    deleteCourse,
    setSearchKeyword
  }
}) 